package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ Description NursingProjectServiceImpl
 * @ Author zhengsuhang
 * @ Date 2025-05-08  11:04
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    // /**
    //  * 护理项目分页查询(方式一)
    //  * @param nursingProjectDto
    //  * @return
    //  */
    // public PageResponse<NursingProjectVo> pageNursingProjectQuery(NursingProjectDto nursingProjectDto) {
    //     PageHelper.startPage(nursingProjectDto.getPageNum(),nursingProjectDto.getPageSize());
    //     Page<NursingProjectVo> p = nursingProjectMapper.pageNursingProjectQuery(nursingProjectDto);
    //     return new PageResponse<NursingProjectVo>(p.getTotal(), p.getResult());
    // }

    /**
     * 护理项目分页查询(方式二)
     * @param nursingProjectDto
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> pageNursingProjectQuery(NursingProjectDto nursingProjectDto) {
        // 调用PageHelper工具类进行分页查询, 入参(开始页码,每页数据条数)
        PageHelper.startPage(nursingProjectDto.getPageNum(),nursingProjectDto.getPageSize());
        // 调用Mapper层进行分页查询, 返回Page<NursingProject>对象
        Page<NursingProject> nursingProjectQuery = nursingProjectMapper.pageNursingProjectQuery(nursingProjectDto);
        // 调用PageResponse的静态方法of()来进行总条数和当前页面数据的封装返回,并将NursingProject转换NursingProjectVo进行类型转换
        PageResponse<NursingProjectVo> projectVoPageResponse = PageResponse.of(nursingProjectQuery, NursingProjectVo.class);
        return projectVoPageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProject
     */
    @Override
    public void addNursingProject(NursingProject nursingProject) {
        // 补全基础字段
        nursingProject.setCreateBy(UserThreadLocal.getMgtUserId());
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(UserThreadLocal.getMgtUserId());
        nursingProject.setUpdateTime(LocalDateTime.now());
        // 调用Mapper层新增护理项目
        nursingProjectMapper.addNursingProject(nursingProject);
    }

    /**
     * 根据id查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Integer id) {
        // 调用Mapper层根据id查询护理项目,返回封装的实体了
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        // 类型转换成NursingProjectVo, 用BeanUtil[hutool的]工具类的toBean()方法
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        // 返回数据
        return nursingProjectVo;
    }

    /**
     * 根据id查询护理项目
     * @param nursingProject
     */
    @Override
    public void updateNursingProject(NursingProject nursingProject) {
        // 补全基础字段
        nursingProject.setUpdateBy(UserThreadLocal.getMgtUserId());
        nursingProject.setUpdateTime(LocalDateTime.now());
        // 调用Mapper层更新护理项目
        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void setSwitchStatus(Long id, Integer status) {
        // 调用Mapper层设置状态
        nursingProjectMapper.setSwitchStatus(id, status);
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> findAll() {
        // 调用Mapper层查询所有
        List<NursingProjectVo> list = nursingProjectMapper.findAll();
        // 返回数据查询到的所有数据
        return list;
    }
}
